#!/usr/bin/perl

die "\nUsage:\n\tfilter_by_node_length.pl <nodelength> file1 [file2..]\n\n" unless @ARGV >=2;
my $node_len = shift @ARGV;
my @FILES = @ARGV;
for my $file(@FILES)
{
	{
		@ARGV = $file;
		my @lines;
		while (<>)
		{
			chomp;
			my @filtered_genes;
			my @genes = split /\t/;
			if (!@genes)
			{
				push(@lines, $_."\n");
				next;
			}
			for (@genes)
			{
				my ($gene_name, $len) = split/:/;
				next unless defined $len and $len <= $node_len;
				push(@filtered_genes, $gene_name);
			}
			
			push(@lines, join("\t", @filtered_genes) . "\n")
				if (@filtered_genes);
		}
		open FILE, ">$file" or die "Could not open $file\n$!\n";
		print FILE @lines;
	}
}
